package com.sql.parser.lineage.utils;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.TokenStreamRewriter;

public class ReplaceTokenUtils {

    /**
     * 反引号->双引号
     *
     * @param replaceStr 要处理字符串
     * @param context    context
     * @param rewriter   rewriter
     * @return 处理后的字符串
     */
    public static String replaceBackQuote(String replaceStr, ParserRuleContext context, TokenStreamRewriter rewriter) {
        if (replaceStr.startsWith("`")) {
            replaceStr = replaceStr.replace("`", "\"");
            rewriter.replace(context.getStart(), context.getStop(), replaceStr);
            return replaceStr;
        }
        return replaceStr;
    }

    /**
     * 双引号->反引号
     *
     * @param replaceStr 要处理字符串
     * @param context    context
     * @param rewriter   rewriter
     * @return 处理后的字符串
     */
    public static String replaceDoubleQuote(String replaceStr, ParserRuleContext context, TokenStreamRewriter rewriter) {
        if (replaceStr.contains("\"")) {
            replaceStr = replaceStr.replace("\"", "'");
            rewriter.replace(context.getStart(), context.getStop(), replaceStr);
            return replaceStr;
        }
        return replaceStr;
    }


//    /**
//     * 利用反射设置指定对象的指定属性为指定的值
//     *
//     * @param obj        目标对象
//     * @param fieldName  目标属性
//     * @param fieldValue 目标值
//     */
//    public static void setFieldValue(Object obj, String fieldName, Object fieldValue) throws IllegalAccessException {
//        Field field = getField(obj, fieldName);
//        if (field != null) {
//            field.setAccessible(true);
//            field.set(obj, fieldValue);
//        }
//    }
}
